<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<jsp:directive.page import="java.io.File" />
<jsp:directive.page import="com.sky.common.string.StringUtils" />
<jsp:directive.page import="com.sky.common.file.FileUtils" />
<jsp:directive.page import="com.sky.common.file.CompressorUtils" />
<%
	String action = request.getParameter("action");
	String directory = StringUtils.isEmpty(request.getParameter("directory")) ? FileUtils.getWebRootPath(): request.getParameter("directory");
	String includeDirectoryRegex = StringUtils.isEmpty(request.getParameter("includeDirectoryRegex")) ? "" : request.getParameter("includeDirectoryRegex");
	String excludeDirectoryRegex = StringUtils.isEmpty(request.getParameter("excludeDirectoryRegex")) ? "(monkey)|(my97datepicker)|(datepicker)|(struts)|(editor)" : request.getParameter("excludeDirectoryRegex");
	String includeFileRegex = StringUtils.isEmpty(request.getParameter("includeFileRegex")) ? "[\\w|\\-|\\_|/|\\\\|\\.\\d]*((.js)|(.css)){1}" : request.getParameter("includeFileRegex");
	String excludeFileRegex = StringUtils.isEmpty(request.getParameter("excludeFileRegex")) ? "MyWindow.buttons.extends.js" : request.getParameter("excludeFileRegex");
	String suffix = StringUtils.isEmpty(request.getParameter("suffix")) ? ".min": request.getParameter("suffix");
	System.out.println("directory:[" + directory + "]\tincludeDirectoryRegex:[" + includeDirectoryRegex+ "]\texcludeDirectoryRegex:[" + excludeDirectoryRegex+ "]\tincludeFileRegex:[" + includeFileRegex+ "]\texcludeFileRegex:[" + excludeFileRegex+ "]\tsuffix:[" + suffix + "]");
	List<File> results = new ArrayList<File>();
	String error = null;
	if (!StringUtils.isEmpty(action)) {
		try {
			results = CompressorUtils.compressDirectory(new File(directory),includeDirectoryRegex,excludeDirectoryRegex,includeFileRegex,excludeFileRegex, suffix);
		} catch (Exception e) {
			error = e.getMessage();
		}
	}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>压缩工具</title>
		<%@include file="/share/header.jsp"%>
		<style>
			*{
				font-size: 12px;
			}
			.input{
				padding: 0;
				margin: 0;
				height:100%;
				width: 100%;
			}
		</style>
	</head>
	<body>
		<form name="myform" method="POST" action="Compressor.jsp">
			<input type="hidden" name="action" value="">
			<table border="0" align="center" cellpadding="0" cellspacing="0">
				<tr>
					<td colspan="4" align="center">
						<hr>
						<font style="font-weight: bold;font-size: 20px;color: green;">压缩工具</font>
						<hr>
					</td>
				</tr>
				<%
				if (error != null) {
				%>
				<tr>
					<td colspan="101" align="center">
						<font style="color: red;"><%=error%> </font>
						<hr>
					</td>
				</tr>
				<%
				}
				%>
				<tr>
					<td align="right" nowrap width="20%">
						<strong>压缩文件所在路径</strong>：
					</td>
					<td nowrap width="30%">
						<input type="text" name="directory" value="<%=directory%>" class="input">
					</td>
					<td align="right" nowrap  width="20%">
						<strong>压缩后追加文件后缀</strong>：
					</td>
					<td width="30%">
						<input type="text" name="suffix" value="<%=suffix%>" class="input">
					</td>					
				</tr>
				<tr>
					<td align="right" nowrap  width="20%">
						<strong>压缩包含文件夹正则表达式</strong>：
					</td>
					<td nowrap  width="30%">
						<input type="text" name="includeDirectoryRegex" value="<%=includeDirectoryRegex%>" class="input">
					</td>
					<td align="right" nowrap  width="20%">
						<strong>压缩排除文件夹正则表达式</strong>：
					</td>
					<td width="30%">
						<input type="text" name="excludeDirectoryRegex" value="<%=excludeDirectoryRegex%>" class="input">
					</td>
				</tr>				
				<tr>
					<td align="right" nowrap  width="20%">
						<strong>压缩包含文件名正则表达式</strong>：
					</td>
					<td nowrap  width="30%">
						<input type="text" name="includeFileRegex" value="<%=includeFileRegex%>" class="input">
					</td>
					<td align="right" nowrap  width="20%">
						<strong>压缩排除文件名正则表达式</strong>：
					</td>
					<td width="30%">
						<input type="text" name="excludeFileRegex" value="<%=excludeFileRegex%>" class="input">
					</td>
				</tr>
				<tr>
					<td colspan="4" align="center">
						<input type="submit" value="开始压缩" onclick="document.myform.action.value='compress';">
						<input type="reset" value="清空">
					</td>
				</tr>
				<tr>
					<td colspan="4" align="center">
						<hr>
					</td>
				</tr>				
				<tr>
					<td colspan="4" align="left">
						<%
						for(File file:results){ 
							out.println(file.length());
							out.println(":\t");
							out.println(file.getPath());
							out.println("<br>");
						}
						%>
					</td>
				</tr>
			</table>
		</form>

	</body>
</html>
